package cn.kgc.crowdfundingadminspringboot2.controller;

import cn.kgc.crowdfundingadminspringboot2.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot2.pojo.Role;
import cn.kgc.crowdfundingadminspringboot2.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleService roleService;

    @RequestMapping("/to/page.html")
    public String toPage() {
        return "role-page";
    }

    @RequestMapping("/search/by/keyword")
    @ResponseBody
    public ResultEntity<PageInfo<Role>> searchBYkeyword(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "5", required = false) Integer pageSize,
                                                        @RequestParam(value = "keyword", defaultValue = "", required = false) String keyword) {

        PageInfo<Role> rolePageInfo = roleService.queryForKeywordWithPage(pageNum, pageSize, keyword);
        ResultEntity<PageInfo<Role>> pageInfoResultEntity = ResultEntity.successWithoutData(rolePageInfo);
        return pageInfoResultEntity;
    }
    @ResponseBody
    @RequestMapping("/get/list/by/id/list")
    public ResultEntity<List<Role>> getRoleListByIdList(@RequestBody List<Integer> roleIdList) {
        List<Role> roleListByIdList = roleService.getRoleListByIdList(roleIdList);
        return ResultEntity.successWithoutData(roleListByIdList);
    }
    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> roleIdList) {
    roleService.batchRemove(roleIdList);
    return ResultEntity.successWithoutData();
    }



    @ResponseBody
    @RequestMapping("/save/role")
    public ResultEntity<String> saveRole(@RequestParam("roleName") String roleName) {

        roleService.saveRole(roleName);

        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/update/role")
    public ResultEntity<String> updateRole(Role role) {

        roleService.updateRole(role);

        return ResultEntity.successWithoutData();
    }
}
